'use strict';

var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');
Object.defineProperty(exports, '__esModule', {
  value: true,
});
exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require('@babel/runtime/helpers/objectSpread2'));
var _objectWithoutProperties2 = _interopRequireDefault(
  require('@babel/runtime/helpers/objectWithoutProperties'),
);
var _react = _interopRequireDefault(require('react'));
var _icons = require('@ant-design/icons');
var _antd = require('antd');
var _Form = _interopRequireDefault(require('../../../Form'));
var _WhenWrap = _interopRequireDefault(require('../../../FormItem/WhenWrap'));
var _SchemaFormContext = require('../../contexts/SchemaFormContext');
var _FormAttrsContext = require('../../../contexts/FormAttrsContext');
require('./index.css');
var _excluded = ['name', 'label', 'description', 'fields', 'form', 'max', 'min', 'when', 'style'],
  _excluded2 = ['fieldType', 'transformProps'];
function AntdList(_ref) {
  var name = _ref.name,
    label = _ref.label,
    description = _ref.description,
    fields = _ref.fields,
    form = _ref.form,
    max = _ref.max,
    min = _ref.min,
    when = _ref.when,
    style = _ref.style,
    rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var _useSchemaFormContext = (0, _SchemaFormContext.useSchemaFormContext)(),
    Fields = _useSchemaFormContext.Fields,
    FieldWrap = _useSchemaFormContext.FieldWrap;
  var _useFormAttrsContext = (0, _FormAttrsContext.useFormAttrsContext)(),
    detail = _useFormAttrsContext.detail;
  var renderFormList = function renderFormList() {
    return /*#__PURE__*/ _react.default.createElement(
      _Form.default.List,
      (0, _objectSpread2.default)(
        {
          name: name,
        },
        rest,
      ),
      function (listFields, _ref2) {
        var add = _ref2.add,
          remove = _ref2.remove;
        return /*#__PURE__*/ _react.default.createElement(
          _react.default.Fragment,
          null,
          listFields.map(function (_ref3, index) {
            var key = _ref3.key,
              name = _ref3.name;
            return /*#__PURE__*/ _react.default.createElement(
              'div',
              {
                key: key,
                className: 'szzj-list-item',
              },
              /*#__PURE__*/ _react.default.createElement(
                'div',
                {
                  className: 'szzj-list-item-header',
                },
                /*#__PURE__*/ _react.default.createElement(
                  'span',
                  null,
                  ''.concat(label).concat(index + 1),
                ),
                (!min || listFields.length > min) &&
                  !detail &&
                  /*#__PURE__*/ _react.default.createElement(
                    'span',
                    {
                      className: 'szzj-list-item-remove-wrap',
                      onClick: function onClick() {
                        return remove(name);
                      },
                    },
                    /*#__PURE__*/ _react.default.createElement(_icons.MinusCircleOutlined, {
                      className: 'szzj-list-item-remove-icon',
                    }),
                    '\u5220\u9664',
                  ),
              ),
              fields.map(function (field) {
                var _field$fieldType = field.fieldType,
                  fieldType = _field$fieldType === void 0 ? 'input' : _field$fieldType,
                  transformProps = field.transformProps,
                  fieldProps = (0, _objectWithoutProperties2.default)(field, _excluded2);
                var FieldComp = Fields === null || Fields === void 0 ? void 0 : Fields[fieldType];
                var innerProps = {};
                if (
                  ['input', 'textarea', 'select', 'cascader', 'tree-select'].includes(fieldType)
                ) {
                  innerProps.allowClear =
                    fieldProps.allowClear === undefined ? true : fieldProps.allowClear;
                }
                var fieldNode = /*#__PURE__*/ _react.default.createElement(
                  FieldComp,
                  (0, _objectSpread2.default)(
                    (0, _objectSpread2.default)(
                      (0, _objectSpread2.default)({}, innerProps),
                      {},
                      {
                        required: true,
                      },
                      transformProps ? transformProps(fieldProps, index) : fieldProps,
                    ),
                    {},
                    {
                      name: [name, fieldProps.name],
                      key: fieldProps.key || fieldProps.name,
                      form: form,
                    },
                  ),
                );
                return FieldWrap
                  ? /*#__PURE__*/ _react.default.createElement(
                      FieldWrap,
                      {
                        key: fieldProps.key || fieldProps.name,
                      },
                      fieldNode,
                    )
                  : fieldNode;
              }),
            );
          }),
          (!max || listFields.length < max) &&
            !detail &&
            /*#__PURE__*/ _react.default.createElement(
              _Form.default.Item,
              {
                label: /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null),
                required: false,
                colon: false,
              },
              /*#__PURE__*/ _react.default.createElement(
                _antd.Button,
                {
                  type: 'dashed',
                  onClick: function onClick() {
                    return add();
                  },
                  block: true,
                  icon: /*#__PURE__*/ _react.default.createElement(_icons.PlusOutlined, null),
                },
                '\u6DFB\u52A0',
                label,
              ),
            ),
        );
      },
    );
  };
  return /*#__PURE__*/ _react.default.createElement(
    _WhenWrap.default,
    {
      when: when,
    },
    /*#__PURE__*/ _react.default.createElement(
      'div',
      {
        className: 'szzj-list',
        style: style,
      },
      /*#__PURE__*/ _react.default.createElement(
        'div',
        {
          className: 'szzj-list-header',
        },
        /*#__PURE__*/ _react.default.createElement('span', {
          className: 'szzj-list-header-mark',
        }),
        /*#__PURE__*/ _react.default.createElement(
          'span',
          {
            className: 'szzj-list-header-title',
          },
          label,
        ),
      ),
      description &&
        /*#__PURE__*/ _react.default.createElement(
          'div',
          {
            className: 'szzj-list-desc',
          },
          description,
        ),
      renderFormList(),
    ),
  );
}
var _default = AntdList;
exports.default = _default;
